package model

import (
	"time"
)

// LoginLog 登录日志模型
type LoginLog struct {
	BaseModel
	UserID        uint      `json:"user_id" gorm:"index;comment:用户ID"`
	Username      string    `json:"username" gorm:"size:50;comment:用户名"`
	LoginType     string    `json:"login_type" gorm:"size:20;comment:登录类型(账号密码、手机验证码、微信等)"`
	LoginIP       string    `json:"login_ip" gorm:"size:50;comment:登录IP"`
	LoginLocation string    `json:"login_location" gorm:"size:100;comment:登录地点"`
	UserAgent     string    `json:"user_agent" gorm:"size:500;comment:用户代理信息"`
	Browser       string    `json:"browser" gorm:"size:50;comment:浏览器类型"`
	OS            string    `json:"os" gorm:"size:50;comment:操作系统"`
	Status        int       `json:"status" gorm:"comment:登录状态(0:失败,1:成功)"`
	Msg           string    `json:"msg" gorm:"size:255;comment:消息提示"`
	LoginTime     time.Time `json:"login_time" gorm:"comment:登录时间"`
	IsAbnormal    int       `json:"is_abnormal" gorm:"default:0;comment:是否异常登录(0:正常,1:异常)"`
	AbnormalType  string    `json:"abnormal_type" gorm:"size:50;comment:异常类型(异地登录、短时间内多次登录等)"`
}

// TableName 表名
func (LoginLog) TableName() string {
	return "sys_login_log"
}

// LoginLogService 登录日志服务接口
type LoginLogService interface {
	// 基础CRUD
	Create(log *LoginLog) error
	GetByID(id uint) (*LoginLog, error)
	List(page, pageSize int, query map[string]interface{}) ([]*LoginLog, int64, error)

	// 业务方法
	RecordLoginLog(userID uint, username, loginType, loginIP, userAgent string, status int, msg string) error
	GetUserLoginLogs(userID uint, page, pageSize int) ([]*LoginLog, int64, error)
	GetRecentLoginLogs(userID uint, limit int) ([]*LoginLog, error)
	GetAbnormalLoginLogs(page, pageSize int) ([]*LoginLog, int64, error)
	ClearLoginLogs(days int) error
}
